//
//  GTNetworking.swift
//  swiftWEIbo
//
//  Created by mac－gt on 15/11/24.
//  Copyright © 2015年 mac－gt. All rights reserved.
//

import UIKit
import AFNetworking
class GTNetworking: AFHTTPSessionManager {
     // 网络单例
    static let sharedInsatance: GTNetworking={
        let baseURL=NSURL(string: "https://api.weibo.com/")
        let tools=GTNetworking(baseURL: baseURL)
        //能够支持解析的数据类型
        tools.responseSerializer.acceptableContentTypes=NSSet(objects: "application/json", "text/json", "text/javascript", "text/plain") as! Set<String>
        return tools
    }()
    
    let client_id="803716600"
    let redirect_uri="http://www.baidu.com/"
      private let grant_type = "authorization_code"
    private let client_secret = "e70c41e61019452147d69bee9b43e2c1"
    // 返回url
    func loadUrl()->NSURL{
        
      let urlString="https://api.weibo.com/oauth2/authorize?client_id=\(client_id)&redirect_uri=\(redirect_uri)"
        
        return NSURL(string: urlString)!
      
    }
    //加载AccessToken
    func loadAccessToken(code: String, finished: ((resut: [String:AnyObject]?, error: NSError?) -> ())){
        let urlString="https://api.weibo.com/oauth2/access_token"
        // 需要向服务器发送的参数
        let parameters=[
        "client_id": client_id,
        "client_secret":client_secret,
        "grant_type":grant_type,
            "code":code,
         "redirect_uri":redirect_uri]
        //  请求类型
        POST(urlString, parameters: parameters, success: { (_, result) -> Void in

            finished(resut: result as? [String:AnyObject] , error: nil)
            }) { (_, error) -> Void in
            
                finished(resut: nil, error: error)
        }
//        requestPOST(urlString, parameters: parameters, finished: finished)
    }
    // 加载用户信息
    func loadUserInfo(finshed:(result: [String: AnyObject]?, error: NSError?) -> ()){
        guard var parameters=tokenDict() else{
            let error=NSError(domain: "没有access_Token", code: -1, userInfo: nil)
            finshed(result: nil, error: error)
            return
        }
        if GTAcounts.loadAccount()?.uid==nil{
            let error=NSError(domain: "没有uid", code: -2, userInfo: nil)
            finshed(result: nil, error: error)
            return
        }
            let urlString = "https://api.weibo.com/2/users/show.json"
        parameters["uid"] = GTAcounts.loadAccount()?.uid
        GET(urlString, parameters: parameters, success: { (_, result) -> Void in
            finshed(result: result as? [String : AnyObject], error: nil)
            }) { (_, error) -> Void in
                finshed(result: nil, error: error)
        }
//        requestGET(urlString, parameters: parameters, finshed: finshed)
    }
    func tokenDict() -> [String: AnyObject]? {
        if GTAcounts.loadAccount()?.access_token == nil {
            return nil
        }
        
        return ["access_token": GTAcounts.loadAccount()!.access_token!]
    }
    //  加载微博数据
    func loadStatus(since_id:Int,max_id:Int, finshed:(resut:[String: AnyObject]?, error: NSError?)->()){
        
        guard let access_token=GTAcounts.loadAccount()?.access_token else{
            let error=NSError(domain: "access_token没有值", code: -1, userInfo: nil)
            finshed(resut: nil, error: error)
            return
        }
        let urlString="2/statuses/home_timeline.json"
        var parameters = [String: AnyObject]()
        parameters["access_token"]=access_token
        if since_id > 0{
             parameters["since_id"] = since_id
        }else if max_id>0{
             parameters["max_id"] = max_id 
        }
        print("\(parameters)")
        GET(urlString, parameters: parameters, success: { (_, resut) -> Void in
            finshed(resut: resut as? [String : AnyObject], error: nil)
            }) { (_, error) -> Void in
            finshed(resut: nil, error: error)
        }
//        requestGET(urlString, parameters:nil, finished: finshed)https://api.weibo.com/2/statuses/home_timeline.json?access_Token=2.00hYfqRG0y5_5sa9e9268b08gmJjKD
    
}
}